package com.cn.shuangzi.ad.custom;

import android.content.Context;

import com.bytedance.sdk.openadsdk.AdSlot;
import com.bytedance.sdk.openadsdk.mediation.bridge.custom.native_ad.MediationCustomNativeLoader;
import com.bytedance.sdk.openadsdk.mediation.custom.MediationCustomServiceConfig;
import com.cn.shuangzi.ad.ADManager;
import com.cn.shuangzi.ad.bean.KsErrorInfo;
import com.cn.shuangzi.ad.util.ThreadUtils;
import com.cn.shuangzi.util.SZUtil;
import com.kwad.sdk.api.KsAdSDK;
import com.kwad.sdk.api.KsFeedAd;
import com.kwad.sdk.api.KsLoadManager;
import com.kwad.sdk.api.KsNativeAd;
import com.kwad.sdk.api.KsScene;
import com.kwad.sdk.api.model.AdExposureFailedReason;
import com.kwad.sdk.api.model.AdExposureFailureCode;
import com.kwad.sdk.api.model.AdnType;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by CN.
 */
public class KsCustomerNative extends MediationCustomNativeLoader {
    private Context mContext;

    @Override
    public void load(final Context context, final AdSlot adSlot, final MediationCustomServiceConfig serviceConfig) {
//        getAdm();
//        getExtraDataNoParse();
        SZUtil.logError("加载快手信息流" + serviceConfig.getADNNetworkSlotId());
        ThreadUtils.runOnThreadPool(new Runnable() {
            @Override
            public void run() {
                mContext = context;
                if (KsAdSDK.getLoadManager() != null) {
                    long posId;
                    try {
                        posId = Long.parseLong(serviceConfig.getADNNetworkSlotId()); //广告位id
                    } catch (Exception e) {
                        callLoadFail(-1, "error");
                        return;
                    }

                    KsScene scene = new KsScene.Builder(posId).width(adSlot.getImgAcceptedWidth()).adNum(adSlot.getAdCount()).build();
                    if (isNativeAd()) {
                        SZUtil.logError("快手信息流，自渲染类型");
                        //自渲染类型
                        KsAdSDK.getLoadManager().loadNativeAd(scene, new KsLoadManager.NativeAdListener() {
                            @Override
                            public void onError(int i, String s) {
                                ADManager.getInstance().setNativeKsErrorInfo(new KsErrorInfo(AdExposureFailureCode.EMPTY));
                                SZUtil.logError("快手信息流，自渲染广告失败：code:" + i + "   msg:" + s);
                                callLoadFail(9999, "快手信息流，没有返回数据");
                            }

                            @Override
                            public void onNativeAdLoad(List<KsNativeAd> list) {
                                if (list == null|| list.isEmpty()) {
                                    ADManager.getInstance().setNativeKsErrorInfo(new KsErrorInfo(new AdExposureFailedReason().setAdnType(AdnType.EMPTY_DATA)));
                                    callLoadFail(9999, "快手信息流，没有返回数据");
                                    SZUtil.logError("快手信息流，自渲染广告广告成功，但没有返回数据");
                                    return;
                                }
                                SZUtil.logError("快手信息流，自渲染广告广告成功，数量:" + list.size());
                                List<KsFeedNativeAd> adList = new ArrayList<>();
                                for (KsNativeAd ad : list) {
                                    KsFeedNativeAd ksFeedNativeAd = new KsFeedNativeAd(mContext, ad);
                                    double ecpm = ad.getECPM();//当无权限调用该接口时，SDK会返回错误码-1
                                    if (ecpm < 0) {
                                        ecpm = 0;
                                    }
                                    SZUtil.logError("快手信息流，ecpm:" + ecpm);
                                    ksFeedNativeAd.setBiddingPrice(ecpm); //回传竞价广告价格
                                    adList.add(ksFeedNativeAd);
                                }
                                ADManager.getInstance().setNativeKsErrorInfo(new KsErrorInfo(new AdExposureFailedReason().setAdnType(AdnType.THIRD_PARTY_AD)));
                                callLoadSuccess(adList);

                            }
                        });
                    } else if (isExpressRender()) {
                        SZUtil.logError("快手信息流，模板类型");
                        //模板类型
                        KsAdSDK.getLoadManager().loadConfigFeedAd(scene, new KsLoadManager.FeedAdListener() {
                            @Override
                            public void onError(int i, String s) {
                                ADManager.getInstance().setNativeKsErrorInfo(new KsErrorInfo(AdExposureFailureCode.EMPTY));
                                SZUtil.logError("快手信息流，onNoAD errorCode = " + i + " errorMessage = " + s);
//                                callLoadFail(9999, "快手信息流，没有返回数据");
                                callLoadFail(i,s);
                            }

                            @Override
                            public void onFeedAdLoad(List<KsFeedAd> list) {
                                if (list == null || list.isEmpty()) {
                                    ADManager.getInstance().setNativeKsErrorInfo(new KsErrorInfo(new AdExposureFailedReason().setAdnType(AdnType.EMPTY_DATA)));
                                    SZUtil.logError("快手信息流，模板广告成功，但没有返回数据");
                                    callLoadFail(9999, "快手信息流，没有返回数据");
                                    return;
                                }
                                SZUtil.logError("快手信息流，模板广告成功，数量:" + list.size());
                                List<KsFeedNativeExpressAd> adList = new ArrayList<>();
                                for (KsFeedAd ad : list) {
                                    KsFeedNativeExpressAd ksFeedNativeExpressAd = new KsFeedNativeExpressAd(mContext, ad);
                                    double ecpm = ad.getECPM();//当无权限调用该接口时，SDK会返回错误码-1
                                    if (ecpm < 0) {
                                        ecpm = 0;
                                    }
                                    SZUtil.logError("快手信息流，ecpm:" + ecpm);
                                    ksFeedNativeExpressAd.setBiddingPrice(ecpm); //回传竞价广告价格
                                    adList.add(ksFeedNativeExpressAd);
                                }
                                ADManager.getInstance().setNativeKsErrorInfo(new KsErrorInfo(new AdExposureFailedReason().setAdnType(AdnType.THIRD_PARTY_AD)));
                                callLoadSuccess(adList);
                            }
                        });
                    } else {
                        SZUtil.logError("快手信息流，其他类型");
                        //其他类型，开发者如果有需要，请在平台自行配置json,然后通过 serviceConfig.getCustomAdapterJson() 获取配置
                    }
                }

            }
        });
    }

    @Override
    public void receiveBidResult(boolean win, double winnerPrice, int loseReason, Map<String, Object> extra) {

    }

}
